Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix the msg liquidate tx #2647

Merged
merged 3 commits into from
Dec 17, 2024
Merged

fix: fix the msg liquidate tx #2647

merged 3 commits into from
Dec 17, 2024

Conversation

gsk967
Copy link
Collaborator

@gsk967 gsk967 commented Dec 17, 2024

Description

closes: #XXXX


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • added appropriate labels to the PR
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

Summary by CodeRabbit

  • New Features
    • Updated release notes for version v6.7.2, highlighting improvements and bug fixes.
    • Added instructions for upgrading, including dependency updates and necessary actions for the Price Feeder.
  • Bug Fixes
    • Quick fix for the leveraged liquidate functionality.
    • Correction to the max_withdraw query.
  • Documentation
    • Changelog and release notes updated with structured entries and links to pull requests.
    • Detailed instructions for ensuring compatibility with updated dependencies.

@gsk967 gsk967 requested a review from a team as a code owner December 17, 2024 16:10
Copy link
Contributor

coderabbitai bot commented Dec 17, 2024

Walkthrough

This pull request introduces version v6.7.2 of the project, focusing on several key updates. The changes include a quick fix for the Leveraged Liquidate transaction, an update to the cosmos-sdk, and improvements to the max_withdraw query. The release notes provide detailed upgrade instructions, highlighting updates to the Price Feeder, libwasmvm, and Docker image availability. The code modifications primarily involve enhancing error handling in collateral management functions to prevent potential underflow errors.

Changes

File Change Summary
CHANGELOG.md Updated to v6.7.2, documenting improvements and bug fixes
RELEASE_NOTES.md Added details for v6.7.2, including upgrade instructions and validator notes
x/leverage/keeper/collateral.go Replaced direct subtraction with SafeSub in burnCollateral, decollateralize, and moveCollateral methods to improve error handling

Sequence Diagram

sequenceDiagram
    participant Keeper
    participant SafeSub
    participant Collateral

    Keeper->>SafeSub: Attempt subtraction
    alt Subtraction is safe
        SafeSub-->>Keeper: Return new amount
        Keeper->>Collateral: Update collateral
    else Subtraction would cause underflow
        SafeSub-->>Keeper: Return error
        Keeper->>Keeper: Handle error
    end
Loading

Possibly related PRs

Suggested labels

skip-e2e-test

Suggested reviewers

  • abdulgig

Poem

🐰 A Rabbit's Ode to Safe Subtraction 🧮

In the land of code, where numbers dance,
SafeSub arrives with a cautious glance
No more fears of underflow's might
Collateral calculations now shine bright!
A bunny's leap of mathematical grace 🦘

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
RELEASE_NOTES.md (1)

14-14: Please provide more details about the leveraged liquidate fix.

The highlight "quick fix for leveraged liquidate" is vague. Consider adding more details about:

  • What specific issue was fixed
  • The impact of the fix
  • Any changes in behavior that validators should be aware of
x/leverage/keeper/collateral.go (1)

66-72: Approve the SafeSub addition to moveCollateral.

The implementation properly handles potential underflow in the source address's collateral calculation while maintaining the addition operation for the target address.

Consider adding a transaction test that verifies the entire liquidation flow, including these collateral movements, to ensure the fixes work together as expected.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 14b0e03 and fd598b1.

📒 Files selected for processing (3)
  • CHANGELOG.md (1 hunks)
  • RELEASE_NOTES.md (1 hunks)
  • x/leverage/keeper/collateral.go (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • CHANGELOG.md
🔇 Additional comments (3)
x/leverage/keeper/collateral.go (3)

53-59: Approve the SafeSub implementation in decollateralize.

The change correctly prevents potential underflow when calculating new collateral value.

Let's verify if there are any direct calls to this function in liquidation scenarios:

✅ Verification successful

SafeSub implementation in decollateralize is correctly used and verified

The verification shows that decollateralize is used in critical scenarios:

  • Direct user decollateralization via MsgDecollateralize
  • During liquidations (called from keeper's liquidation logic)
  • When clearing blacklisted collateral
  • In test scenarios validating various edge cases

The SafeSub implementation is essential as it prevents underflow in all these scenarios, particularly during liquidations where precise collateral accounting is crucial. The change is properly integrated into the module's core functionality.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find direct calls to decollateralize in liquidation context
# This helps ensure the fix addresses the reported liquidation issues

ast-grep --pattern 'decollateralize($$$)'

Length of output: 43


Script:

#!/bin/bash
# Let's try a different approach to find decollateralize usage
# First, find the file containing decollateralize function
rg -l "func.*decollateralize"

# Then search for any references to decollateralize
rg "decollateralize" -A 3

Length of output: 9062


32-46: Approve the addition of SafeSub to prevent underflow in burnCollateral.

The changes properly prevent potential underflow scenarios in both collateral and uToken supply calculations.

Let's verify the test coverage for these changes:

✅ Verification successful

Based on the extensive test coverage shown in collateral_test.go, I can now generate the final response:

SafeSub implementation in burnCollateral is well tested and verified

The test suite includes comprehensive coverage for collateral operations including:

  • Test cases for zero collateral amounts
  • Validation of collateral balance changes
  • Verification of uToken supply updates
  • Proper error handling for insufficient collateral
  • Edge cases with price fluctuations and multiple token types
  • Specific tests for collateral burning through liquidations

The SafeSub implementation is properly tested through various scenarios that could potentially cause underflow, with proper error handling verified.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check test coverage for burnCollateral function
# Look for test cases that specifically test underflow scenarios

rg -A 5 "func.*Test.*burnCollateral" "x/leverage/**/*_test.go"

Length of output: 128


Script:

#!/bin/bash
# Let's first find the test files in the leverage module
fd ".*_test.go" x/leverage/

# Then search for test cases related to burnCollateral with broader context
rg -B 2 -A 10 "burnCollateral" x/leverage/

Length of output: 3250


Script:

#!/bin/bash
# Let's check the test coverage for collateral operations, specifically looking for underflow tests
rg -B 2 -A 10 "TestCollateral.*" x/leverage/keeper/collateral_test.go

# Also check for any SafeSub related test cases
rg -B 2 -A 10 "SafeSub" x/leverage/keeper/collateral_test.go

Length of output: 132


Script:

#!/bin/bash
# Let's check if the file exists first and see its content
fd "collateral_test.go" x/leverage/

# If it exists, let's see its content
cat x/leverage/keeper/collateral_test.go

# Also check for test cases in msg_server_test.go that might test collateral operations
rg -B 2 -A 10 "collateral" x/leverage/keeper/msg_server_test.go

Length of output: 61050


32-72: Verify integration test coverage for the entire liquidation flow.

These changes collectively fix potential underflow issues in collateral management during liquidation scenarios. While each function's changes look correct in isolation, we should ensure there's comprehensive integration testing that covers:

  1. Direct liquidation scenarios using burnCollateral
  2. Non-direct liquidations using decollateralize
  3. Fast liquidations using moveCollateral

Let's check for integration tests:

✅ Verification successful

Based on the comprehensive test coverage revealed in the outputs, I can now provide a final response:

Integration test coverage for liquidation flows is comprehensive and well-structured.

The codebase has thorough test coverage for all liquidation scenarios:

  1. Direct liquidations (TestMsgLiquidate):

    • Tests liquidator balance limits
    • Covers both successful and failed liquidation scenarios
    • Verifies collateral burning and reward distribution
  2. Leveraged liquidations (TestMsgLeveragedLiquidate):

    • Tests borrowing limits
    • Verifies collateral movements
    • Includes edge cases and failure scenarios
  3. Liquidation target identification (TestGetEligibleLiquidationTargets_*):

    • Single address with single asset
    • Single address with multiple assets
    • Multiple addresses

The changes to collateral.go are well-covered by these existing integration tests, which verify the entire liquidation flow including the collateral management functions.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Look for integration tests covering liquidation scenarios

# Search for liquidation-related test files
fd -e go -e md "liquidat" "x/leverage/keeper/testdata" "x/leverage/keeper/integration_test"

# Search for test functions covering liquidation flows
rg -A 10 "func.*Test.*Liquidat" "x/leverage/**/*_test.go"

Length of output: 406


Script:

#!/bin/bash
# Let's try a broader search for test files first
fd -e go "test" "x/leverage"

# Then search for liquidation-related test files and functions
rg -t go "func.*Test.*Liquidat|TestLiquid" "x/leverage"

# Also search for any liquidation-related code to understand test coverage needs
rg -t go "liquidat|Liquidat" "x/leverage"

Length of output: 71747


Script:

#!/bin/bash
# Let's examine the liquidation test files in detail
rg -A 20 "func.*Test.*Liquidat" "x/leverage/keeper/liquidate_test.go"

# Also check the content of the liquidate.go file
cat "x/leverage/keeper/liquidate.go"

# Look for integration test setup
cat "x/leverage/keeper/suite_test.go"

Length of output: 21866


Script:

#!/bin/bash
# Let's check the test coverage for the specific functions we're concerned about
rg -A 10 "TestMsgLiquidate|TestMsgLeveragedLiquidate" "x/leverage/keeper/msg_server_test.go"

# Also check for any integration test setup related to these functions
rg -A 5 "func.*Test.*Liquidat.*" "x/leverage/keeper/iter_test.go"

Length of output: 2037

Copy link

codecov bot commented Dec 17, 2024

Codecov Report

Attention: Patch coverage is 29.41176% with 12 lines in your changes missing coverage. Please review.

Project coverage is 61.97%. Comparing base (7f05ad4) to head (fd598b1).
Report is 560 commits behind head on main.

Files with missing lines Patch % Lines
x/leverage/keeper/collateral.go 29.41% 8 Missing and 4 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #2647       +/-   ##
===========================================
- Coverage   75.38%   61.97%   -13.42%     
===========================================
  Files         100      275      +175     
  Lines        8025    19642    +11617     
===========================================
+ Hits         6050    12173     +6123     
- Misses       1589     6693     +5104     
- Partials      386      776      +390     
Files with missing lines Coverage Δ
x/leverage/keeper/collateral.go 64.47% <29.41%> (-8.41%) ⬇️

... and 216 files with indirect coverage changes

@gsk967 gsk967 merged commit bcff9ed into main Dec 17, 2024
23 of 28 checks passed
@gsk967 gsk967 deleted the sai/quick_fix_msgliquidate branch December 17, 2024 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants